const util = require('../../utils/util.js');
const app = getApp()

Page({
  
  data: {
    remainMoney: '',
    buttonTitle: '获取数据...'
  },

  unpermittedPayments: null,
  remainValue: 0,
  accountUpdateTime: 0,

  onLoad: function (options) {
    const page = this;
    page.accountUpdateTime = options.accountUpdateTime;
    page.setData({ hasAccountChange: page.accountUpdateTime > app.globalData.accountUpdateTime });
  },

  onReady: function() {
    // app.changeStyle(this);
    this.fetchContent();
  },

  withdraw: function() {
    const page = this;
    if (page.unpermittedPayments == null) {
      return;
    }
    if (page.unpermittedPayments.length > 0) {
      wx.showModal({
        title: '提示',
        content: '请等待上次到账后再次提现。',
        showCancel: false
      });
      return;
    }
    if (page.remainValue <= 100) {
      wx.showModal({
        title: '错误',
        content: '账户上没有足够提现的资金，需要多于 1.00 元。',
        showCancel: false
      });
    } else {
      wx.showModal({
        title: '提取到微信账户',
        content: '是否提取' + page.data.remainMoney + '到个人微信账户？',
        showCancel: true,
        success: (res) => {
          if (res.confirm) { page.doWithdraw(); }
        }
      });
    }
  },

  doWithdraw: function() {
    const page = this;
    wx.request({
      url: app.processUri('accounts/_/payments'),
      method: "POST",
      data: { paymentAccountId: app.globalData.accountId, paymentPostId: 0, value: -page.remainValue },
      header: { 'Authorization': app.globalData.accessToken },
      success: (res) => {
        console.log(res);
        if (res.statusCode >= 200 && res.statusCode < 300 && res.data != null) {
          let content = '本次提现金额' + util.formatChineseCurrency(-res.data.value);
          if (res.data['fee'] != null) content += '，扣除手续费' + util.formatChineseCurrency(res.data.fee);
          wx.showModal({
            title: '提现成功',
            content: content,
            showCancel: false
          });
          page.updateRemainValue(0);
        } else {
          wx.showToast({ title: res.statusCode.toString() });
        }
      },
      fail: () => {
        wx.showToast({ title: '提现失败' });
      }
    });
  },

  updateRemainValue: function(value) {
    const page = this;
    if (value == null) value = 0; else value = parseInt(value);
    page.remainValue = value;
    page.setData({ remainMoney: util.formatChineseCurrency(value) });
  },

  fetchContent: function() {
    const page = this;
    wx.request({
      url: app.processUri('accounts/_'),
      method: "GET",
      header: { 'Authorization': app.globalData.accessToken },
      success: function (res) {
        page.updateRemainValue(res.data['value']);
      }
    });
    wx.request({
      url: app.processUri('accounts/_/payments/unpermitted'),
      method: "GET",
      header: { 'Authorization': app.globalData.accessToken },
      success: function (res) {
        if (res.statusCode < 200 || res.statusCode >= 300 || res.data == null) {
          page.unpermittedPayments = null;
        } else {
          page.unpermittedPayments = res.data.filter(payment => {
            return payment.value < 0;
          });
        }
      },
      complete: function() {
        let title;
        if (page.unpermittedPayments == null) {
          title = '无法获取账户数据';
        } else if (page.unpermittedPayments.length > 0) {
          title = '提款到账中...';
        } else if (page.remainValue < 100) {
          title = '提现余额不足';
        } else {
          title = '提现';
        }
        page.setData({buttonTitle:title});
      }
    });
  },

  showBill: function (event) {
    wx.navigateTo({
      url: '/pages/bill/bill',
    });
    app.globalData.accountUpdateTime = this.accountUpdateTime;
    this.setData({ hasAccountChange: false });
  }

})
